# -------------------------------------------------------------------
# Purpose: Creates Table B52
# Author:  Max Posch, 25/07/2025
# Usage:   Source this script to generate the table.
# -------------------------------------------------------------------
# Check that required paths exist
stopifnot(dir.exists(pdataanalysis))
stopifnot(dir.exists(poutputappendix))


# Load data
load(file.path(pdataanalysis, "countyLevel19001940.RData"))


# Subset data
d1 <- countyLevel19001940[!is.na(entropy_occ1950_adjp)]
d2 <- countyLevel19001940[!is.na(entropy_uspto_techn)]
d3 <- countyLevel19001940[!is.na(segregation_namelast_mp_adjp) & !is.na(segregation_coo_adjp)]
d4 <- countyLevel19001940[!is.na(sft_pc1)]

# Winsorize and normalize variables
vars <- names(countyLevel19001940) %>% 
  str_subset("entropy|sum_n_namelast_mp_adjp|^segregation|sft|n_uspto_techn") %>% 
  str_subset(., "_ws$", negate = TRUE)
d1[, paste0(vars, "_ws") := lapply(.SD, function(x) scale(winsorize(x, probs = c(.01, .99)))), .SDcols = vars]
d2[, paste0(vars, "_ws") := lapply(.SD, function(x) scale(winsorize(x, probs = c(.01, .99)))), .SDcols = vars]
d3[, paste0(vars, "_ws") := lapply(.SD, function(x) scale(winsorize(x, probs = c(.01, .99)))), .SDcols = vars]
d4[, paste0(vars, "_ws") := lapply(.SD, function(x) scale(winsorize(x, probs = c(.01, .99)))), .SDcols = vars]


# Regressions
o <- list()
o <- append(o, list(feols(entropy_occ1950_adjp_ws ~ entropy_namelast_mp_adjp_ws + sum_n_namelast_mp_adjp_ws | gisjoin_1900 + statefip^year + gisjoin_1900[year], d1)))
o <- append(o, list(feols(entropy_occ1950_adjp_ws ~ entropy_namelast_mp_adjp_ws + sum_n_namelast_mp_adjp_ws + entropy_cob_adjp_ws | gisjoin_1900 + statefip^year + gisjoin_1900[year], d1)))
o <- append(o, list(feols(entropy_uspto_techn_ws ~ entropy_namelast_mp_adjp_ws + sum_n_namelast_mp_adjp_ws | gisjoin_1900 + statefip^year + gisjoin_1900[year], d2)))
o <- append(o, list(feols(entropy_uspto_techn_ws ~ entropy_namelast_mp_adjp_ws + sum_n_namelast_mp_adjp_ws + entropy_cob_adjp_ws | gisjoin_1900 + statefip^year + gisjoin_1900[year], d2)))
o <- append(o, list(feols(segregation_namelast_mp_adjp_ws ~ entropy_namelast_mp_adjp_ws + sum_n_namelast_mp_adjp_ws | gisjoin_1900 + statefip^year + gisjoin_1900[year], d3)))
o <- append(o, list(feols(segregation_namelast_mp_adjp_ws ~ entropy_namelast_mp_adjp_ws + sum_n_namelast_mp_adjp_ws + entropy_cob_adjp_ws | gisjoin_1900 + statefip^year + gisjoin_1900[year], d3)))
o <- append(o, list(feols(segregation_coo_adjp_ws ~ entropy_namelast_mp_adjp_ws + sum_n_namelast_mp_adjp_ws | gisjoin_1900 + statefip^year + gisjoin_1900[year], d3)))
o <- append(o, list(feols(segregation_coo_adjp_ws ~ entropy_namelast_mp_adjp_ws + sum_n_namelast_mp_adjp_ws + entropy_cob_adjp_ws | gisjoin_1900 + statefip^year + gisjoin_1900[year], d3)))
o <- append(o, list(feols(sft_pc1_ws ~ entropy_namelast_mp_adjp_ws + sum_n_namelast_mp_adjp_ws | gisjoin_1900 + statefip^year + gisjoin_1900[year], d4)))
o <- append(o, list(feols(sft_pc1_ws ~ entropy_namelast_mp_adjp_ws + sum_n_namelast_mp_adjp_ws + entropy_cob_adjp_ws | gisjoin_1900 + statefip^year + gisjoin_1900[year], d4)))


# Create table
setFixest_dict(
  c(
    entropy_namelast_mp_adjp_ws = "Surname diversity",
    entropy_cob_adjp_ws = "Birth-country diversity",
    sum_n_namelast_mp_adjp_ws = "Population",
    segregation_namelast_mp_adjp_ws = "\\makecell{Residential segregation\\\\of surname groups}",
    segregation_coo_adjp_ws = "\\makecell{Residential segregation of\\\\ ancestral-country groups}",
    entropy_occ1950_adjp_ws = "\\makecell{Occupational\\\\diversity}",
    entropy_uspto_techn_ws = "\\makecell{USPTO tech \\\\codes diversity}",
    sft_pc1_ws = "\\makecell{Strength of\\\\family ties}",
    year = "Period", statefip = "State", gisjoin_1900 = "County"
  )
)


tablename <- file.path(poutputappendix, "tableB52.tex")
etable(o,
  cluster = ~statefip,
  fitstat = ~n, digits = "r3", digits.stats = "r3",
  order = c("diversity"),
  file = tablename, replace = TRUE,
  style.tex = style.tex("aer"), tex = TRUE
)
edit_table_content_fixed(tablename, "Period $\\times $ County", "County-specific linear trends")
add_table_row(tablename, "\\makecell", "\\cmidrule(lr){2-3}  \\cmidrule(lr){4-5} \\cmidrule(lr){6-7}  \\cmidrule(lr){8-9}  \\cmidrule(lr){10-11}")
move_table_row(tablename, "Observations", "bottomrule")
writeLines(readLines(tablename)[-16], tablename)

cat("Table B52 saved to:", tablename, "\n")